package me.sauce.dntest;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import me.sauce.dntest.data.TimerModel;

/**
 * Created by sauce on 2017/4/27.
 * Version 1.0.0
 */
public class TimerListAdapter extends RecyclerView.Adapter<TimerListAdapter.ViewHolder> {
    private List<TimerModel> timerModelList = new ArrayList<>();
    private TimerListAdapter.onItemClick onItemClick;

    public TimerListAdapter(TimerListAdapter.onItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_timer, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final TimerModel timerModel = timerModelList.get(position);
        holder.title.setText(timerModel.getTitle());
        holder.period.setText(String.valueOf(timerModel.getPeriod()));
        holder.color.setText(timerModel.getColor());
        holder.color.setTextColor(Color.parseColor(timerModel.getColor()));
        holder.enable.setChecked(timerModel.isEnable());
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onItemClick != null) {
                    onItemClick.onClick(timerModel.getId());
                }
                return true;
            }
        });
    }

    public void setTimerModelList(List<TimerModel> timerModelList) {
        this.timerModelList = timerModelList;
    }

    @Override
    public int getItemCount() {
        return timerModelList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        TextView period;
        TextView color;
        CheckBox enable;

        ViewHolder(View itemView) {
            super(itemView);

            title = (TextView) itemView.findViewById(R.id.title);
            period = (TextView) itemView.findViewById(R.id.period);
            color = (TextView) itemView.findViewById(R.id.color);
            enable = (CheckBox) itemView.findViewById(R.id.enable);

        }
    }

    interface onItemClick {
        void onClick(long id);
    }
}
